package com.wang.mall.listener;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.wang.mall.service.OmsOrderService;
import com.wang.mall.to.rabbitmq.SeckillOrderTo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "wang-order-seckill-order-queue")
public class OrderSeckillListener {

    @Autowired
    OmsOrderService omsOrderService;

    @RabbitHandler
    public void listenerSeckillOrder(String seckillOrderToString, Message message, Channel channel) throws IOException {
        System.out.println("收到秒杀单信息");
        try {
            SeckillOrderTo seckillOrderTo = JSON.parseObject(seckillOrderToString, SeckillOrderTo.class);
            omsOrderService.createdSeckillOrder(seckillOrderTo);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
